package com.intelligent.health.base.excption;

import entity.R;
import entity.ResultCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/04/02/0:17
 * @Description: 统一异常处理
 */
@ControllerAdvice
public class ControllerExceptionHandler {

    /**
     * 文件导出异常统一处理 FileExportException
     * 请求参数缺失异常统一处理 RequestException
     *
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        if (e instanceof FileExportException) {
            return R.error().message(e.getMessage());
        }
        if (e instanceof RequestException || e instanceof EmailSendException) {
            return R.error().message(e.getMessage());
        }
        return R.setResult(ResultCodeEnum.SERVER_ERROR);
    }
}
